package zcw.com.lib_leet_code;

/**
 * Created by 朱城委 on 2021/7/14.<br><br>
 *
 * 猜数字大小
 */
public class Topic374 {

    private int number;

    public static void main(String[] args) {
        Topic374 instance = new Topic374();

        instance.number = 6;
        System.out.println(instance.guessNumber(10));

        instance.number = 1;
        System.out.println(instance.guessNumber(1));

        instance.number = 1;
        System.out.println(instance.guessNumber(2));

        instance.number = 2;
        System.out.println(instance.guessNumber(2));

        instance.number = 1702766719;
        System.out.println(instance.guessNumber(2126753390));
    }

    public int guessNumber(int n) {
        int low = 1;
        int high = n;

        while (low < high) {
            int mild = (int) (((long) low + high) / 2);

            if(guess(mild) == 0) {
                return mild;
            }
            else if(guess(mild) > 0) {
                low = mild + 1;
            }
            else {
                high = mild - 1;
            }
        }

        return low;
    }

    private int guess(int num) {
        if(number < num) {
            return -1;
        }
        else if(number > num) {
            return 1;
        }

        return 0;
    }
}
